//
//  HWHomeMoreCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/11/13.
//  Copyright © 2023 94Wan. All rights reserved.
//

import UIKit

class HWHomeMoreCell: UITableViewCell {
    var collectionView:UICollectionView!
    let collectionProtocol = HWHomeMoreProtocol()
    
    var model:HWHomeModel? {
        didSet {
            collectionProtocol.model = model
            collectionView.reloadData()
            
        }
    }
    class func cellWithTableView(_ tableView:UITableView) -> (HWHomeMoreCell) {
        let reUse:String = "HWHomeMoreCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWHomeMoreCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWHomeMoreCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.white
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(HWHomeMoreCollectionCell.self, forCellWithReuseIdentifier: "HWHomeMoreCollectionCell")
        contentView.addSubview(collectionView)

    }
    
    func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview().priority(.high)
            make.height.equalTo(118*widthScale)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

class HWHomeMoreProtocol: NSObject {
    var model:HWHomeModel?
//    var didClickHeavyItem:((_:HWHomeTuiJianModel)->())?
}

extension HWHomeMoreProtocol:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        self.didClickHeavyItem?(model?.tuijian?[indexPath.row] ?? HWHomeTuiJianModel())
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 13*widthScale, bottom: 0, right: 13*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10*widthScale
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 12*widthScale
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 80*widthScale, height: 100*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  model?.fenlei?.count ?? 0
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        for i in 0..<(model?.fenlei?.count ?? 0) {
            print(model?.fenlei?[i].name ?? "")
        }
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWHomeMoreCollectionCell", for: indexPath) as! HWHomeMoreCollectionCell
        collectionCell.titleLabel.text = model?.fenlei?[indexPath.row].name
        collectionCell.bgImageView.kf.setImage(with: URL(string: model?.fenlei?[indexPath.row].icon ?? ""))
        return collectionCell
    }
    
}


class HWHomeMoreCollectionCell: UICollectionViewCell {
    var bgImageView:UIImageView!
    var titleLabel:UILabel!
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWHomeMoreCollectionCell {
    private func initUI() -> Void {
        contentView.backgroundColor = .clear
        
        bgImageView = UIImageView()
        bgImageView.backgroundColor = .clear
        bgImageView.layer.cornerRadius = 18*widthScale
        bgImageView.layer.masksToBounds = true
        bgImageView.contentMode = .scaleAspectFill
        contentView.addSubview(bgImageView)

        
        titleLabel = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 11*widthScale)
        titleLabel.textAlignment = .center
        bgImageView.addSubview(titleLabel)
        
    }
    
    private func initFrame() -> Void {
        
        bgImageView.snp.makeConstraints { make in
            make.top.equalTo(18*widthScale)
            make.bottom.left.right.equalToSuperview()
//            make.height.equalTo(159*widthScale).priority(.high)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-7*widthScale)
            make.height.equalTo(20*widthScale)
        }

    }
}

